package com.thtf.homework.util;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

// 应该直接注解Data，写的时候忘了
public class TreeNode {
    private String id;
    private String label;
    private String icon;
    private List<TreeNode> children;

    public TreeNode() {}

    public TreeNode(String id, String label, String icon) {
        this.id = id;
        this.label = label;
        this.icon = icon;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public List<TreeNode> getChildren() {
        if (children == null) {
            children = new ArrayList<>();
        }
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        // 使用StringBuilder来构建字符串，因为它比字符串连接（+）更高效
        StringBuilder sb = new StringBuilder();
        sb.append("TreeNode{");
        sb.append("id=").append(id);
        sb.append(", label='").append(label).append('\'');
        sb.append(", icon='").append(icon).append('\'');
        sb.append(", children=[");

        // 如果children不为空，则使用stream()来生成子节点的字符串表示
        // 使用Collectors.joining(", ")来将每个子节点的字符串表示用逗号分隔
        if (children != null && !children.isEmpty()) {
            sb.append(children.stream()
                    .map(TreeNode::toString)
                    .collect(Collectors.joining(", ")));
        }

        // 如果没有子节点或children为null，则添加空列表的字符串表示
        if (children == null || children.isEmpty()) {
            sb.append("[]");
        }

        sb.append('}');
        return sb.toString();
    }
}